// pages/mine/index.js
const App = getApp();
const dayjs = require('dayjs');
let sys = wx.getSystemInfoSync();
let isiOS = false;
if (sys.system.indexOf('iOS') > -1 || sys.model.indexOf('iPhone') > -1) {
    isiOS = true;
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: wx.getStorageSync('user'),
        products: [],
        product1:{},
        product3:{},
        promotion: {},
        isiOS,
        isOnline: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            user: wx.getStorageSync('user')
        });
        
         //this.getproduct()
        // this.replayPromotion()
    },
    onShow() {
        let {
            user,
            products
        } = this.data;
        if (!user) {
            this.setData({
                user: wx.getStorageSync('user')
            })
        }
        if (user) {
            //this.getproduct()
        }
        //判断客服在线时间段
        const now = dayjs();
        // 今天的 09:00
        const start = dayjs().hour(9).minute(0).second(0);
        // 今天的 20:59
        const end = dayjs().hour(20).minute(59).second(59);
        const isInRange = now >= start && now <= end;
        console.log(isInRange);  
      // 使用示例：判断当前时间是否在早上 9 点 00 分至晚上 20 点 59 分之间
      this.setData({ isOnline: isInRange });
    },
    async getproduct() {
        App.showLoading();
        let {
            data
        } = await App.$http("/api/front/productList");
        this.setData({
            products: data,
            product1:data.find(n => n.name=='vip1'),
            product3:data.find(n => n.name=='vip3'),
            loading: false
        });
        wx.hideLoading();
    },
    async replayPromotion() {
        App.showLoading();
        let {
            data
        } = await App.$http("/api/front/promotion");
        this.setData({
            promotion: data,
            loading: false
        });
        wx.hideLoading();
    },
    toExChange() {
        let isLogin = wx.getStorageSync('token');
        if (!isLogin) {
            this.setData({
                showDialog2: true,
                tipsText: '用户未登录，无法使用此功能',
            });
            return
        }
        wx.navigateTo({
            url: `/pages/exchange/exchange`,
        });
    },
    toProduct(e) {
        let {
            user,
            products,
            isiOS
        } = this.data;
        // if (isiOS) {
        //     this.setData({
        //         showDialog2: true,
        //         tipsText: '该功能iOS不支持，请在安卓手机操作',
        //     });
        //     return
        // }
        if (!user && !products.length) {
            this.setData({
                showDialog2: true,
                tipsText: '用户未登录，无法使用此功能',
            });
            return
        }
        // let {
        //     currentTarget: {
        //         dataset: {
        //             type
        //         }
        //     }
        // } = e;
        // let [item] = this.data.products.filter(n => n.usetype == type);
        // wx.navigateTo({
        //     url: `plugin-private://wx34345ae5855f892d/pages/productDetail/productDetail?productId=${item.detailid}`,
        // });

        wx.redirectTo({
          url: `/pages/product/index?pagecode=/pages/mine/index`,
      });

    },
    toOrderList() {
        let isLogin = wx.getStorageSync('token');
        if (!isLogin) {
            this.setData({
                showDialog2: true,
                tipsText: '用户未登录，无法使用此功能',
            });
            return
        }
        wx.navigateTo({
            url: 'plugin-private://wx34345ae5855f892d/pages/orderList/orderList',
        });
    },
    onUpDialog() {
        this.setData({
            showDialog: true
        });
    },
    exit() {
        wx.clearStorageSync();
        this.setData({
            user: ''
        })
        wx.redirectTo({
            url: `/pages/login/index?promotioncode=${wx.getStorageSync('promotioncode')}`,
        });
    },
    login() {
        wx.navigateTo({
            url: `/pages/login/index?promotioncode=${wx.getStorageSync('promotioncode')}`,
        });
    },
    handleContact() {
        this.setData({
            showDialog2: true,
            tipsText: '客服在线时间段：09:00-20:59',
        });
    }
})